/*
 * Copyright (C) 2012-2013  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.remote.rsa.data;

import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.remoteserviceadmin.EndpointDescription;
import org.osgi.service.remoteserviceadmin.ImportReference;

/**
 * ImportReferenceImpl : The {@link ImportReference} implementation.
 * 
 * @author Romain DUBOIS
 */
public class ImportReferenceImpl extends
        ReferenceImpl<ServiceRegistration<?>, ImportRegistrationImpl> implements ImportReference {

    /**
     * Constructor
     * 
     * @param pRegistration
     *            The service proxy registration
     * @param pEndpoint
     *            The endpoint description
     * @param pListener
     *            The listener to notify
     */
    public ImportReferenceImpl(final ServiceRegistration<?> pRegistration,
            final EndpointDescription pEndpoint, final IUnregistrationListener pListener) {
        super(pEndpoint, pRegistration, pListener);
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.service.remoteserviceadmin.ImportReference#getImportedService()
     */
    @Override
    public synchronized ServiceReference<?> getImportedService() {
        if (getObject() == null) {
            return null;
        }
        return getObject().getReference();
    }

    /**
     * {@inheritDoc}
     * 
     * @see org.osgi.service.remoteserviceadmin.ImportReference#getImportedEndpoint()
     */
    @Override
    public synchronized EndpointDescription getImportedEndpoint() {
        return getEndpoint();
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.data.ReferenceImpl#registrationClosed()
     */
    @Override
    public synchronized void registrationClosed() {
        final ServiceRegistration<?> lRegistration = getObject();
        super.registrationClosed();
        if (!isActive() && lRegistration != null) {
            lRegistration.unregister();
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.data.ReferenceImpl#createRegistration()
     */
    @Override
    protected ImportRegistrationImpl createRegistration() {
        return new ImportRegistrationImpl(this);
    }

    /**
     * {@inheritDoc}
     * 
     * @see nemos.remote.rsa.data.ReferenceImpl#notify(nemos.remote.rsa.data.IUnregistrationListener)
     */
    @Override
    protected void notify(final IUnregistrationListener pListener) {
        pListener.importUnregistered(this);
    }
}
